package services

import (
	"context"
	"github.com/pkg/errors"
	"go.didapinche.com/foundation/plat-echo/app/ops/models"
	"go.didapinche.com/foundation/plat-echo/app/ops/repositories"
	"go.didapinche.com/foundation/plat-echo/app/ops/zclients"
	"go.didapinche.com/goapi/host_service_api"
	"go.didapinche.com/goapi/plat_limos_rpc"
	"go.didapinche.com/time"
	"go.uber.org/zap"
)

// HostService define interface of host service
type HostService interface {
	SyncHostResource(ctx context.Context) error
}

type hostService struct {
	logger          *zap.Logger
	graphRepository repositories.GraphRepository
	limos           plat_limos_rpc.TChanLimosService
	k8s             K8sService
	host            host_service_api.TChanHostService
	template        TemplateService
	hostRepository  repositories.HostRepository
}

// NewHostService is constructor of host service
func NewHostService(
	logger *zap.Logger,
	k8s K8sService,
	template TemplateService,
	graphRepository repositories.GraphRepository,
	host host_service_api.TChanHostService,
	limos plat_limos_rpc.TChanLimosService,
	hostRepository repositories.HostRepository,
) HostService {
	h := &hostService{
		limos:           limos,
		host:            host,
		graphRepository: graphRepository,
		logger:          logger.With(zap.String("Type", "services.host")),
		hostRepository:  hostRepository,
		k8s:             k8s,
		template:        template,
	}
	return h
}

func (s *hostService) SyncHostResource(ctx context.Context) error {
	result, err := zclients.FindHost2(ctx, s.host)
	if err != nil {
		err = errors.Wrapf(err, "call zclients.FindHost()")
		s.logger.Error(err.Error())
		return err
	}
	//删除所有现存数据
	err = s.hostRepository.DeleteAll(ctx)
	if err != nil {
		err = errors.Wrapf(err, "call hostRepository.DeleteAll()")
		s.logger.Error(err.Error())
		return err
	}
	err = s.graphRepository.DeleteHost(ctx)
	if err != nil {
		err = errors.Wrapf(err, "call graphRepository.DeleteHost error")
		s.logger.Error(err.Error())
		return err
	}
	//数据入库
	for _, h := range result.Hosts {
		host := new(models.HostResource)
		host.CreateTime = time.Now()
		host.Name = h.Name
		host.IP = h.IP
		host.EnvID = uint64(h.EnvID)
		if host.EnvID == 1 {
			host.Env = "TEST"
		} else if host.EnvID == 4 {
			host.Env = "ALIYUN"
		} else {
			host.Env = "ONLINE"
		}

		host.Group = h.UsageGroup
		host.HostName = h.Name
		host.DC = h.Dc
		host.IPOutside = h.IP
		//host.Score = uint64(h.Score)
		err = s.hostRepository.Create(ctx, host)
		if err != nil {
			err = errors.Wrapf(err, "call hostRepository.Create()")
			s.logger.Error(err.Error())
			return err
		}
		templates, err := s.template.FindByHostID(ctx, host.ID)
		if err != nil {
			s.logger.Error("call templateService FindByHostID error", zap.Error(err))
		}
		graphs := s.template.ChangeTemplate(ctx, templates, host.ID, nil)
		if err := s.graphRepository.Creates(ctx, graphs); err != nil {
			s.logger.Error("call templateService RefreshTemplate error:s.graphRepository.Creates", zap.Error(err))
		}
		//s.template.UpdateK8s(ctx, int(host.ID), 2, host.IP)
	}
	return nil
}
